package it.gat.client.view;

import it.gat.controller.UtenteController;
import it.gat.controller.utils.GatSession;
import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPasswordField;

public class LoginDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	
	private final JPanel contentPanel = new JPanel();
	private JTextField textNomeUtente;
	private JPasswordField textPassword;

	/**
	 * Create the dialog.
	 */
	public LoginDialog(final UtenteController uc,final JFrame mainFrame) {
		super(mainFrame,true);
		setTitle("Login");
		setBounds(100, 100, 247, 138);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNomeUtente = new JLabel("Nome Utente");
			lblNomeUtente.setBounds(10, 11, 63, 14);
			contentPanel.add(lblNomeUtente);
		}
		{
			JLabel lblPassword = new JLabel("Password");
			lblPassword.setBounds(10, 36, 46, 14);
			contentPanel.add(lblPassword);
		}
		{
			textNomeUtente = new JTextField();
			textNomeUtente.setBounds(83, 8, 140, 20);
			contentPanel.add(textNomeUtente);
			textNomeUtente.setColumns(10);
		}
		{
			textPassword = new JPasswordField();
			textPassword.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if (e.getKeyCode()==KeyEvent.VK_ENTER)
					{
						GatSession.getInstance().setNomeUtente(textNomeUtente.getText());
						GatSession.getInstance().setPassword(String.valueOf(textPassword.getPassword()));
						dispose();
					}
				}
			});
			textPassword.setBounds(83, 33, 140, 20);
			contentPanel.add(textPassword);
			textPassword.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						GatSession.getInstance().setNomeUtente(textNomeUtente.getText());
						GatSession.getInstance().setPassword(String.valueOf(textPassword.getPassword()));
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		textNomeUtente.setText("admin");
		textPassword.setText("admin");
	}

}
